#ifndef __VIDEO_FRAME_POOL_H_
#define __VIDEO_FRAME_POOL_H_

#include "video_memory.h"
#include <bitset>

namespace video
{

using namespace std;

#define MAX_SOLTS (8)


enum SOLT_TYPE{
    SOLT_TYPE_VIDEO=0,
    SOLT_TYPE_AUDIO,
    SOLT_TYPE_BUFFER
};

class IMemoryPool
{
private:
    /* data */
public:
    static IMemoryPool *getInstance();
    virtual ~IMemoryPool() {}

    virtual int32_t createSolt(int32_t *solt_id, int32_t buf_size, int32_t frame_type, int32_t max_num) = 0;
    virtual int32_t createSolt(int32_t *solt_id, int32_t w, int32_t h, int32_t w_stride, int32_t h_stride, int32_t frame_type, int32_t max_num) = 0;
    virtual int32_t destorySolt(int32_t solt_id) = 0;

    virtual IMemory *get(int32_t solt_id) = 0;
    virtual bool put(int32_t solt_id, IMemory *mem) = 0;
};



class MemoryPoolImpl : public IMemoryPool, public IMemoryMgr
{
private:
    struct MemorySolt
    {
        int32_t id;
        SOLT_TYPE solt_type;
        std::vector<IMemory*> mem;
        std::list<IMemory*> mem_used;
        std::list<IMemory*> mem_free;
        union {
            struct
            {   int32_t type;
                int32_t timestamp;
                int32_t size;
            }buffer;

            struct
            {
                int32_t width;
                int32_t height;
                int32_t width_stride;
                int32_t height_stride;
                int32_t frame_type;
                int32_t timestamp;
            }video;

            struct
            {
                int32_t timestamp;
                int32_t frame_type;
            }audio;
        }u;
        uint32_t max_size;
        uint32_t acture_size;
    };

    std::map<int32_t, MemorySolt*> _solts;
    std::bitset<MAX_SOLTS> _solts_bm;
    int32_t find_fist_zero_solt();

public:
    MemoryPoolImpl(/* args */);
    virtual ~MemoryPoolImpl();
    virtual int32_t createSolt(int32_t *solt_id, int32_t buf_size, int32_t frame_type, int32_t max_num);
    virtual int32_t createSolt(int32_t *solt_id, int32_t w, int32_t h, int32_t w_stride, int32_t h_stride, int32_t frame_type, int32_t max_num);
    virtual int32_t destorySolt(int32_t solt_id);

    virtual IMemory *get(int32_t solt_id);
    virtual bool put(int32_t solt_id, IMemory *mem);

    virtual void signal_mem_release( IMemory *mem, void *ctx );

};


















} // namespace video

#endif